<?php
	$level = $_GET["level"];
	
	if ($level > 4)
	{
		header("Location: index.php?error=1");
	}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
	<title>Pin Project | Showcase</title>
	<script type="text/javascript" src="javascript/ShowcaseC.js"></script>
	<link rel="stylesheet" type="text/css" href="css/Showcase.css" />
</head>

<?php

	require_once(getenv("DOCUMENT_ROOT")."/common/PinSQL.obj");
	$pinSQL = new PinSQL();

	/*
	possible values for $level:
	(0) Select User
		|__ (1) Select Media type
			|__ (2) Select Album (of type)
				|__ (3) Select Content Item
					|__ (4) Item page displayed
	*/
	$destUser = $_GET["destUser"];
	$destType = $_GET["destType"];
	/* for $destType,
		0: pictures
		1: music
		2: videos
		3: games
		4: blogs
	*/
	$destAlbumId = $_GET["destAlbumId"];
	$destItemId = $_GET["destItemId"];
	
	$error = $_GET['error'];
	$u = "undefined";
	
	$level = (($destUser!=$u)*1 + ($destType!=$u)*1 + ($destAlbumId!=$u)*1 + ($destItemId!=$u)*1);
	echo '<script type="text/javascript">
		parent.level="'.$level.'";
		parent.destUser="'.$destUser.'";
		parent.destType="'.$destType.'";
		parent.destAlbumId="'.$destAlbumId.'";
		parent.destItemId="'.$destItemId.'";
	</script>';
?>

<div class="header">
	<div class="floatLeft">
		<img src="img/LogoSm.png" />
	</div>
	<div class="floatLeft" style="width: 100px;">&nbsp;</div>
	<div class="floatLeft paddedText">
		<div id="headerText"><?php

		switch($level)
		{
			case 0: echo "Choose a user"; break;
			case 1: echo "$destUser's showcase"; break;
			case 2: 
				echo "$destUser's ";
				switch ($destType)
				{
					case 0:		//pictures
						echo "picture";
						break;
					case 1:		//music
						echo "music";
						break;
					case 2:
						echo "video";
						break;
					case 3:
						echo "games";
						break;
					case 4:
						echo "blog";
						break;
				}
				echo " albums"; break;
			case 3: echo "$destUser's album, ";
				echo $pinSQL->GetALbumName($destAlbumId); break;
			case 4: echo "$destUser's ";
				switch ($destType)
				{
					case 0:		//pictures
						echo "picture";
						break;
					case 1:		//music
						echo "music";
						break;
					case 2:
						echo "video";
						break;
					case 3:
						echo "games";
						break;
					case 4:
						echo "blog";
						break;
				}
			echo " , $destItemId"; break;
		}
		
		?></div>
	</div>	
	<div class="floatleft error"><?php if ($error == 1) echo "Please do not use your browser's back button!"; ?></div>
	<div class="clear"></div>
</div>

<?php

//   _____________________
//__/  Output list        \________________________________

	$title = NULL;
	$subtitle = NULL;
	$img = NULL;
	
	switch($level)
	{
		case LEVEL_SELECT_USER:
			$pinSQL->Query("SELECT * FROM members");
			break;
		case LEVEL_SELECT_MEDIA_TYPE:
			$pinSQL->Query("SELECT * FROM picturedata WHERE artist='" . $destUser . "'");
			$pictures = $pinSQL->GetNumRows();
			$pinSQL->Query("SELECT * FROM songdata WHERE artist='" . $destUser . "'");
			$songs = $pinSQL->GetNumRows();
			$pinSQL->Query("SELECT * FROM blogdata WHERE artist='" . $destUser . "'");
			$blogs = $pinSQL->GetNumRows();
			
			break;
		case LEVEL_SELECT_ALBUM:
			$pinSQL->Query("SELECT * FROM albumdata WHERE type='$destType' AND (username='$destUser' OR username='')");
			break;
		case LEVEL_SELECT_ITEM:
			$pinSQL->Query("SELECT * FROM " . $pinSQL->GetTablePrefix($destType) . "data WHERE albumid=$destAlbumId AND artist='$destUser'");
			break;
			
		case LEVEL_VIEW_ITEM:
			$pinSQL->Query("SELECT * FROM " . $pinSQL->GetTablePrefix($destType) . "data WHERE id=$destItemId");
			break;
	}

	$itemId = 0;
	while ( ($level != LEVEL_SELECT_MEDIA_TYPE) && ($row = $pinSQL->FetchNextRow()) )
	{		
		$footer = "";
		switch ($level)
		{
			case LEVEL_SELECT_USER:
				$title = $row['username'];
				$num = $pinSQL->GetNumAlbums($title);
				$subtitle = $num.' album'.(($num != 1) ? 's':'');
				
				
				if (!$picurl = $pinSQL->GetUserdataField($row['username'], 'picurl'))
					$img = "/img/icons/nopic.png";
				else
					$img = "/members/userData/" . $title . "/$picurl";
				
				$pictures = $pinSQL->GetNumMediaType(0, $title);
				$music = $pinSQL->GetNumMediaType(1, $title);
				//$videos = $pinSQL->GetNumMediaType(2, $title);
				//$games = $pinSQL->GetNumMediaType(3, $title);
				$blogs = $pinSQL->GetNumMediaType(4, $title);
				
				
				$footer = '<div style="text-align: right;"><img src="/img/icons/picture_sm.png" />'.$pictures.' | <img src="/img/icons/song_sm.png" />'.$music.' | <img src="/img/icons/entry_sm.png" />'.$blogs.'</div>';
				
				break;
			case LEVEL_SELECT_ALBUM:
				$album = $row['albumId'];
				$title = $row['name'];
				$subtitle = "<numitems>";
				$footer = 'Album rating: <rating>';
				break;
			case LEVEL_SELECT_ITEM:
				$item = $row['id'];
				$title = $row['name'];
				if (strlen($title) > 20)
				{
					$title = substr($title, 0, 25) . "...";
				}
				$subtitle = $row['artist'];
				
				
				
				$totalstars = $pinSQL->GetMediaField($pinSQL->GetTablePrefix($destType).'data', $item, 'totalstars');
				$totalratings = $pinSQL->GetMediaField($pinSQL->GetTablePrefix($destType).'data', $item, 'totalratings');
				if ($totalratings)	{ 
					switch ($destType)	
					{
						case 0:	$footer="Picture"; break;
						case 1: $footer="Song"; break;
						case 2: $footer="Video"; break;
						case 3: $footer="Game"; break;
						case 4: $footer="Blog"; break;	
					}
					$averagestars=round($totalstars / $totalratings / 2, 0);
				
					$gauge='<img src="/common/gallery/img/gauge_'.$averagestars.'.png" />'; 
				
					$footer.=" rating: $gauge";
				}
				else	
				{
					$footer = "no ratings";
				}
		}
		
		if ($img)
		{
			$imageSize = getimagesize(getenv("DOCUMENT_ROOT").$img);
			
			if ($imageSize[1] <= 136)
			{
				$newWidth = $imageSize[0];
				$newHeight = $imageSize[1];
			} else {
				$widthFactor = $imageSize[0] / $imageSize[1];
				$newHeight = 136;
				$newWidth = $newHeight * $widthFactor;
			}
		}

?>

<div class="item floatLeft margin" id="<?php echo $itemId; ?>">
	<div class="listItemSegment"  onmouseover="hoverOn(this);" onmouseout="hoverOff(this);" onclick="clickOn(this);"
		destUser="<?php echo $title; ?>"
		destType="<?php echo $type; ?>"
		destAlbumId="<?php echo $album; ?>"
		destItemId="<?php echo $item; ?>" 
		innerId="<?php echo $itemId++; ?>">
			
		<div class="contentImage">
			<img src="<?php echo $img; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>"/>
		</div>
		<div class="itemTitle"><?php echo $title; ?></div>
		<div class="itemSubtitle"><?php echo $subtitle; ?></div>
	</div>
	
	<div class="listItemSegment footer">
		<div class="info"><?php echo $footer; ?></div>
	</div>
	
	<div class="reflection">&nbsp;</div>
</div>		
		
		
		
<?php		
	}
	
	if ($level == LEVEL_SELECT_MEDIA_TYPE)
	{
		?>
		<div class="item floatLeft margin">
	<div class="listItemSegment"  onmouseover="hoverOn(this);" onmouseout="hoverOff(this);" onclick="clickOn(this);"
		destType="0"
		 >
			
		<div class="contentImage">
			<img src="<?php echo $img; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>"/>
		</div>
		<div class="itemTitle">View Pictures</div>
		<div class="itemSubtitle"><?php echo $pictures; ?> pictures</div>
	</div>
	
	<div class="listItemSegment footer">
	</div>
	
	<div class="reflection">&nbsp;</div>
</div>		<div class="item floatLeft margin">
	<div class="listItemSegment"  onmouseover="hoverOn(this);" onmouseout="hoverOff(this);" onclick="clickOn(this);"
		destType="1"
		 >
			
		<div class="contentImage">
			<img src="<?php echo $img; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>"/>
		</div>
		<div class="itemTitle">View Songs</div>
		<div class="itemSubtitle"><?php echo $songs; ?> songs</div>
	</div>
	
	<div class="listItemSegment footer">
	</div>
	
	<div class="reflection">&nbsp;</div>
</div>		<div class="item floatLeft margin">
	<div class="listItemSegment"  onmouseover="hoverOn(this);" onmouseout="hoverOff(this);" onclick="clickOn(this);"
		destType="4"
		 >
			
		<div class="contentImage">
			<img src="<?php echo $img; ?>" width="<?php echo $newWidth; ?>" height="<?php echo $newHeight; ?>"/>
		</div>
		<div class="itemTitle">View Blogs</div>
		<div class="itemSubtitle"><?php echo $blogs; ?> blog entries</div>
	</div>
	
	<div class="listItemSegment footer">
	</div>
	
	<div class="reflection">&nbsp;</div>
</div>		
<?php
}
		
	
?>


</div>

<!-- end of ListView->outputList !-->

